package com.wenhao.netease.internet;

import org.junit.Test;

public class Main2 {

    @Test
    public void test() {
        System.out.println(findKthBit(4,11));
    }

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 返回Sn的第k位字符
     * @param n int整型 Sn的n
     * @param k int整型 需要返回的字符下标位
     * @return char字符型
     */
    public char findKthBit (int n, int k) {
        // write code here
        result[0] = "a";
        String string = getString(n);
        return string.charAt(k - 1);
    }

    private String getString (int n) {
        if (result[n - 1] != null) {
            return result[n - 1];
        }
        result[n - 1] = getString(n - 1) + (char)('a' + n - 1) + reversAndInvert(getString(n - 1));
        return result[n - 1];
    }
    private static String[] result = new String[26];
    private static char[] map = new char[]{'z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a'};

    private String reversAndInvert(String s) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = s.length() - 1;i >= 0;i--) {
            stringBuilder.append(map[s.charAt(i) - 'a']);
        }
        return stringBuilder.toString();
    }

}
